package cc.geguol.iodemo.writer;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 作者：13687
 * 日期：2017/6/17
 * 项目：IODemo
 * 描述：读取字符
 * 1. 会一个一个读，知道返回-1
 */
public class FileReaderDemo {
	public static void main(String[] args) {
//		readChar();
		readCharBuff();
	}

	private static void readChar() {
		FileReader fileReader =null;
		try {
			fileReader = new FileReader("fileWriterDemo.md");

//			每次读一个字符
			int ch = 0;
			while ((ch=fileReader.read())!=-1){
				System.out.println("char = "+(char)ch);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileReader!=null) {
					fileReader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	private static void readCharBuff() {
		FileReader fileReader =null;
		try {
			fileReader = new FileReader("fileWriterDemo.md");

//			每次读一个字符
			int ch = 0;
			char[] buff = new char[1024];
			while ((ch=fileReader.read(buff))!=-1){
			    // 因为文件的回车换行已经读取了，所以没必要用 ln
				System.out.print(new String(buff,0,buff.length));
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileReader!=null) {
					fileReader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
